package controller;

import common.Result;
import pojo.DoctorWork;
import pojo.DoctorWorkParameter;
import pojo.FullDoctorWork;
import pojo.Work;
import service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.MissingServletRequestParameterException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Api(tags = "WorkPlanController 相关接口，管理工作计划与医生排班")
@RestController
@RequestMapping("/workPlan")
public class WorkController {

    @Autowired
    private WorkService workPlanService;

    // 问题1修复：明确参数说明 + 异常兜底
    @ApiOperation("按周获取周一到周日的计划大纲，需传入周参数（如'2025-06-W25'）")
    @GetMapping("/getPlansByWeek")
    public Result getPlansByWeek(
            @RequestParam(required = true, value = "week",
                    defaultValue = "2025-06-W25") String week // 可根据业务设默认值
    ) {
        try {
            List<DoctorWork> list = workPlanService.getWorkPlanByWeek(week);
            return Result.ok(list);
        } catch (Exception e) {
            return Result.fail("系统异常：" + e.getMessage());
        }
    }

    @ApiOperation("获取周一到周日的计划大纲状态（含统一返回）")
    @GetMapping("/getPlansStatus2")
    public Result getPlansStatus2(@RequestParam String startDay) {
        Work wp = workPlanService.getPlansStatus(startDay);
        return Result.ok(wp);
    }

    @ApiOperation("获取周一到周日的计划大纲状态（直接返回 WorkPlan）")
    @GetMapping("/getPlansStatus")
    public Work getPlansStatus(@RequestParam String startDay) {
        return workPlanService.getPlansStatus(startDay);
    }

    @ApiOperation("生成周一到周日的计划大纲")
    @PostMapping("/generatePlans")
    public Result generatePlans(@RequestParam String startDay) {
        Work wp = new Work();
        wp.setDay(startDay);
        wp.setStatus(0);
        wp.setCreateBy("admin");
        wp.setWeekday("1");

        boolean res = workPlanService.generatePlan(wp);
        return res ? Result.ok("生成成功") : Result.fail("生成失败");
    }

    @ApiOperation("修改计划大纲的状态")
    @PostMapping("/updatePlanStatus")
    public Result updatePlanStatus(
            @RequestParam String day,
            @RequestParam int status
    ) {
        Work wp = new Work();
        wp.setDay(day);
        wp.setStatus(status);

        int res = workPlanService.updateByDay(wp);
        return res > 0 ? Result.ok("状态修改成功") : Result.fail("状态修改失败");
    }

    @ApiOperation("获取当前周的所有医生排班情况")
    @GetMapping("/getDoctorWorks")
    public Result getDoctorWorks(@RequestParam String day) {
        List<FullDoctorWork> list = workPlanService.getAllBaseInfo(day);
        return Result.ok(list);
    }

    @ApiOperation("更新医生排班信息")
    @PostMapping("/updateDoctorWorks")
    public Result updateDoctorWorks(
            @RequestParam String day,
            @RequestParam int departmentId,
            @RequestBody DoctorWorkParameter parameter
    ) {
        List<DoctorWork> doctors = parameter.getDoctorworks();
        int res = workPlanService.updateDoctorWorks(day, departmentId, doctors);
        return res > 0 ? Result.ok("保存成功") : Result.fail("保存失败");
    }

    private Date getDate(String str) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}